#include <stdio.h>
#include <stdlib.h>


void usage(const char *program, int error_mesg);
int correct_args(int argc, char **argv);


int main(int argc, char *argv[])
{
    int a,b,r;
    int error = correct_args(argc, argv);
    const char *program = argv[0];
    if(error)
    {
        usage(program, error);
        exit(1);
    }
    a = atol(argv[1]);
    b = atol(argv[2]);
    r = a + b;
    printf("%d + %d = %d\n", a, b, r);
}

void usage(const char *program, int error_code)
{
    fprintf(stderr, "Usage: %s <num1> <num2>\n", program);
    const char *error_mesg;
    switch (error_code)
    {
        case 1:
            error_mesg = "wrong number of arguments";
            break;
        case 2:
            error_mesg = "one of the arguments is not a number";
            break;
        default:
            break;
    }
    fprintf(stderr, "ERROR: %s\n", error_mesg);
}

int correct_args(int argc, char **argv)
{
    if(argc != 3)
        return 1;
    for(int i = 1; i < argc; ++i)
        for(char *p = argv[i]; *p; ++p)
            if(*p < '0' || *p > '9')
                return 2;
    return 0;
}
